import { createGroup, getList } from '../../api/group.js'

const group = {
  state: {
    groupList: []
  },
  mutations: {
    UPDATE_GroupList: (state, group) => {
      console.log(state)
      state.groupList.push(group)
    },
    SET_GroupList: (state, list) => {
      state.groupList = list
    }
  },
  actions: {
    // 创建组
    CreateGroup(context, groupInfo) {
      const { groupName, imagePath, username, creater, members, folders, setTime } = groupInfo
      const args = {
        groupName,
        imagePath,
        folders,
        members,
        setTime,
        creater,
        username
      }
      return new Promise((resolve, reject) => {
        createGroup(args).then(response => {
          // console.log('data:', response)
          const { msg } = response

          context.commit('UPDATE_GroupList', args)
          resolve(msg)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取用户所有组信息
    GetGroupInfo(context, username) {
      return new Promise((resolve, reject) => {
        getList(username).then(response => {
          const { groupList } = response
          // console.log(groupList)
          // 更新
          context.commit('UPDATE_GroupList', groupList)

          resolve(groupList)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}

export default group
